<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Shop\Form;

use Admin\Data\Config;
use Laminas\Form\Form;
use Shop\Validator\PhoneCaptchaValidator;
use User\Validator\PhoneValidator;

class UserPhoneLoginForm extends Form
{
    private $phoneArea;

    public function __construct(array $phoneArea, $name = 'user-phone-login-form', $options = [])
    {
        parent::__construct($name, $options);

        $this->setAttribute('method', 'post');

        $this->phoneArea        = $phoneArea;

        $this->addElements();
        $this->addInputFilter();
    }

    protected function addElements()
    {
        if(count($this->phoneArea) == 1) {
            $this->add([
                'type'  => 'hidden',
                'name'  => 'areaCode',
                'attributes'=> [
                    'id'    => 'areaCode',
                    'value' => $this->phoneArea[0]['areaCode']
                ]
            ]);
        } else {
            $this->add([
                'type'  => 'select',
                'name'  => 'areaCode',
                'attributes'=> [
                    'id'    => 'areaCode',
                    'value' => $this->phoneArea['default']
                ],
                'options' => [
                    'value_options' => $this->phoneArea['list']
                ]
            ]);
        }

        $this->add([
            'type'  => 'text',
            'name'  => 'userPhone',
            'attributes'=> [
                'id'    => 'userPhone'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'phoneCaptcha',
            'attributes'=> [
                'id'    => 'phoneCaptcha'
            ]
        ]);

        $this->add([
            'type'  => 'hidden',
            'name'  => 'httpReferer',
            'attributes' => [
                'id'    => 'httpReferer'
            ]
        ]);

        $this->add([
            'type'  => 'csrf',
            'name'  => 'shopCsrf',
            'options' => [
                'csrf_options' => [
                    'timeout'  => Config::SHOP_POST_TOKEN_TIMEOUT
                ]
            ]
        ]);
    }

    protected function addInputFilter()
    {
        $inputFilter = $this->getInputFilter();

        $inputFilter->add([
            'name'      => 'userPhone',
            'required'  => true,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ],
            'validators'=> [
                [
                    'name' => PhoneValidator::class
                ],
                [
                    'name'      => 'StringLength',
                    'options'   => [
                        'min'   => 1,
                        'max'   => 30
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'phoneCaptcha',
            'required'  => true,
            'filters'   => [
                ['name' => 'ToInt']
            ],
            'validators'=> [
                [
                    'name' => PhoneCaptchaValidator::class
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'httpReferer',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim']
            ]
        ]);
    }
}